package cn.skyclass.net
{
	import cn.skyclass.controls.AdvancedAlert;
	import cn.skyclass.events.EventNotification;
	import cn.skyclass.events.SubmitEvent;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	[Event(name="inited", type="cn.skyclass.events.SubmitEvent")]
	[Event(name="submitting", type="cn.skyclass.events.SubmitEvent")]
	[Event(name="submitted", type="cn.skyclass.events.SubmitEvent")]
	[Event(name="failed", type="cn.skyclass.events.SubmitEvent")]
	[Event(name="canceled", type="cn.skyclass.events.SubmitEvent")]
	/**
	 * 基本的逻辑操作类，提供向服务器端提交数据的基本操作和状态控制
	 */
	[Bindable]
	public class BaseSubmitter extends EventDispatcher
	{
		/**
		 * 是否正在向服务器端提交数据
		 */
		private var _submitting:Boolean;
		public function get submitting():Boolean{
			return this._submitting;
		}
		public function set submitting(value:Boolean):void{
			this._submitting = value;
		}
		
		/**
		 * 数据是否已经提交结束
		 */
		private var _submitted:Boolean;
		public function get submitted():Boolean{
			return this._submitted;
		}
		public function set submitted(value:Boolean):void{
			this._submitted = value;
		}
		
		
		/**
		 * 进行逻辑操作的初始化工作
		 */
		public function init():void{
			// for overriding
		}
		
		/**
		 * 执行数据提交工作
		 */
		public function submit():void{
			// 如果正在提交，则不做任何处理，直接返回
			if(this.submitting){ return; }
			
			var msg:String = this.validate();
			if(msg){
				AdvancedAlert.info(msg);
			}else{
				this.dispatchSubmittingEvent();
				this.doSubmit();
			}			
		}
		
		/**
		 * 执行数据提交逻辑
		 */
		protected function doSubmit():void{
			// for overriding
		}
				
		/**
		 * 取消当前的操作
		 */
		public function cancel():void{
			// for overriding
		}
		
		/**
		 * 将自身重置为初始状态
		 */
		public function reset():void{
			this.submitted = false;
			this.submitting = false;
		}
				
		/**
		 * 提供在提交前对当前的数据进行验证
		 */
		public function validate():String{
			// for overriding
			return "";
		}
		
		/**
		 * 某项操作发生时更新自身的状态
		 */
		protected function updateState():void{
			// for overriding
		}
		
		/**
		 * 用于发布和监听与此操作相关的全局事件
		 * 使用默认的 EventNotification
		 */
		protected function get en():EventNotification{
			return EventNotification.getInstance();
		}		
		
		/**
		 * 发布正在提交数据的事件
		 */
		protected function dispatchSubmittingEvent(e:Event=null):void{
			this.submitting = true;
			this.updateState();
			dispatchEvent(new SubmitEvent(SubmitEvent.SUBMITTING));			
		}
		
		/**
		 * 提交数据完毕后发布事件
		 */
		protected function dispatchSubmittedEvent(e:Event=null):void{
			this.submitted = true;
			this.submitting = false;
			this.updateState();
			dispatchEvent(new SubmitEvent(SubmitEvent.SUBMITTED));
		}
		
		/**
		 * 发布数据提交失败时的事件
		 */
		protected function dispatchFailedEvent(e:Event=null):void{
			this.submitted = false;
			this.submitting = false;
			this.updateState();
			dispatchEvent(new SubmitEvent(SubmitEvent.FAILED));
		}		
		
		/**
		 * 发布初始化结束事件
		 */
		protected function dispatchInitedEvent(e:Event=null):void{
			dispatchEvent(new SubmitEvent(SubmitEvent.INITED));			
		}		
		
		/**
		 * 发布取消事件
		 */
		protected function dispatchCanceledEvent(e:Event=null):void{
			dispatchEvent(new SubmitEvent(SubmitEvent.CANCELED));			
		}
				
	}
}